/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

/**
 * Cette classe définit une brique de type Liste qui hérite la classe View
 * et qui implémente l'interface ActionListener.
 * @author NGUYEN Dac Cong Tai
 */
public class ListeView extends View implements ActionListener{
    
    /**
     * Constructeur d'une brique de type Liste en appelant le constructeur de la classe mère.
     * @param brique l'élément contient des informations de la brique
     * @param ecran la dimension de l'écran actuel
     * @param element l'élément contient les textes pour cette brique
     * @param langue le code de la langue d'affichage
     * @param typeProfil le texte indique le type du profil (profil d'élèves ou profil de groupe)
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
    public ListeView(Element brique, Dimension ecran, Element element, String langue, String typeProfil) throws ParserConfigurationException, SAXException, IOException{
        super(brique, ecran, element, langue, typeProfil);
        String nom = brique.getAttribute("nom");

        Element rootEl = perl.Utility.getElement(perl.Utility.getPath(this, "/Fichiers systeme/Parametres/modele.xml"));
        //String hexa = View.getHexaColor(rootEl.getElementsByTagName("type"),"0");
        Color color = View.decodeColor(rootEl.getElementsByTagName("type"),"0");
        setBorder(new LineBorder(color, View.thickness));
        setPreferredSize(new Dimension((ecran.width-50)/3, (ecran.height-155)/2));
        setToolTipText(((Element)element.getElementsByTagName("Brique").item(0)).getElementsByTagName(langue).item(0).getTextContent()+" "+nom);
        

        JTextArea name = new JTextArea(nom);
        name.setForeground(View.getTextColor(color));
        name.setLineWrap(true);
        name.setEditable(false);
        name.setBackground(color);
        name.setFont(View.getTitle());

        String[] operation={((Element)element.getElementsByTagName("Moyenne").item(0)).getElementsByTagName(langue).item(0).getTextContent(),((Element)element.getElementsByTagName("Somme").item(0)).getElementsByTagName(langue).item(0).getTextContent()};

        JPanel comboPanel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.weightx=0.0;

        c.gridy = 0;
        c.gridx = 0;
        NodeList listEchelles = brique.getElementsByTagName("echelle");
        typeOperations = new ArrayList<Boolean>();
        listComboBox = new ArrayList<JComboBox>();
        listMenuVal = new ArrayList<JMenu>();
        for (int i=0; i<listEchelles.getLength(); i++){
            JLabel label = new JLabel(((Element)element.getElementsByTagName("Valeur").item(0)).getElementsByTagName(langue).item(0).getTextContent()+" "+(i+1)+":");
            c.insets= new Insets(0, 0, 0, 0);
            comboPanel.add(label,c);
            c.gridx++;
            JComboBox comboBox = new JComboBox(operation);
            listComboBox.add(comboBox);
            comboBox.setName(String.valueOf(i));
            comboBox.addActionListener(this);
            typeOperations.add(false);
            if(c.gridx==5){
                c.insets= new Insets(0, 0, 0, 0);
                comboPanel.add(comboBox,c);
                c.gridx =0;
                c.gridy++;
            } else {
                c.insets= new Insets(0, 0, 0, 10);
                comboPanel.add(comboBox,c);
                c.gridx ++;
            }
        }

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(name, BorderLayout.NORTH);
        if(c.gridy>1){
            JScrollPane comboScroll = new JScrollPane(comboPanel);
            comboScroll.setBorder(null);
            comboScroll.setPreferredSize(new Dimension(GroupLayout.DEFAULT_SIZE, 40));
            panel.add(comboScroll, BorderLayout.CENTER);
        }else
            panel.add(comboPanel, BorderLayout.CENTER);

        add(panel,BorderLayout.NORTH);

        scrollPane = new JScrollPane(super.createTree(brique, element, langue, typeOperations, typeProfil));
        scrollPane.setBorder(null);
        add(scrollPane, BorderLayout.CENTER);
    }

    /**
     * Définit les actions.
     * @param e l'action
     */
    public void actionPerformed(ActionEvent e) {
        String action = e.getActionCommand();
        JMenuItem menu = null;
        int indexItemSelected=0;
        int indexCB=0;
        if(e.getSource().getClass().equals(JComboBox.class)){
            JComboBox cb = (JComboBox)e.getSource();
            indexItemSelected = cb.getSelectedIndex();
            indexCB = Integer.parseInt(cb.getName());
        } else if(e.getSource().getClass().equals(JMenuItem.class)){
            String[] split = action.split(",");
            indexCB = Integer.parseInt(split[0]);
            indexItemSelected = Integer.parseInt(split[1]);
            menu = (JMenuItem) e.getSource();
            System.out.println("--->>>"+((JPopupMenu)menu.getParent()).getSubElements()[0].getClass());
        }
        
        
        try {
            if (indexItemSelected==0) {
                listComboBox.get(indexCB).setSelectedIndex(0);
                typeOperations.set(indexCB, false);
                scrollPane.getViewport().add(super.createTree(brique, parametre, codeLangue, typeOperations, typeProfil));
                if(menu!=null){
                    ((JMenuItem)((JPopupMenu)menu.getParent()).getSubElements()[0]).setEnabled(false);
                    ((JMenuItem)((JPopupMenu)menu.getParent()).getSubElements()[1]).setEnabled(true);
                } else {
                    listMenuVal.get(indexCB).getItem(0).setEnabled(false);
                    listMenuVal.get(indexCB).getItem(1).setEnabled(true);
                }
            } else if (indexItemSelected==1) {
                listComboBox.get(indexCB).setSelectedIndex(1);
                typeOperations.set(indexCB, true);
                scrollPane.getViewport().add(super.createTree(brique, parametre, codeLangue, typeOperations, typeProfil));
                if(menu!=null){
                    ((JMenuItem)((JPopupMenu)menu.getParent()).getSubElements()[1]).setEnabled(false);
                    ((JMenuItem)((JPopupMenu)menu.getParent()).getSubElements()[0]).setEnabled(true);
                }else {
                    listMenuVal.get(indexCB).getItem(1).setEnabled(false);
                    listMenuVal.get(indexCB).getItem(0).setEnabled(true);
                }
            }
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(ListeView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(ListeView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ListeView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
